from django.shortcuts import get_object_or_404, render
from django.contrib.auth.decorators import login_required
from app.luca_customer.models import *
from datetime import date
from django.contrib import messages
from django.http import HttpResponseRedirect,HttpResponse
from django.utils.translation import ugettext as _
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.core import serializers
from django.core.cache import cache
import json

def customer_new(request):
    group_list = Group.objects.filter(active=1)
    location_list = Location.objects.filter(active=1)
    return render(request, 'new_customer.html',{'group_list':group_list,'location_list': location_list})

def process_new(request):

    if request.POST:
        name = request.POST.get('name')
        telephone = request.POST.get('telephone')
        group = request.POST.get('group')
        location = request.POST.get('location')
        active = request.POST.get('active')
        
        customer = Customer(name=name, telephone=telephone,group_id=group,location_id=location,active=active,created_at=date.today(),updated_at=date.today())
        customer.save() 
        message = name + " " + _("added_success")
        messages.add_message(request, messages.SUCCESS, message)   
        cache.delete('customers_list_json')
    return HttpResponseRedirect('/customer/list')  

def customer_edit(request,customer_id):   
    customer = get_object_or_404(Customer,pk=customer_id)
    group_list = Group.objects.filter(active=1)
    location_list = Location.objects.filter(active=1)
    context = {'customer': customer,'group_list': group_list,'location_list': location_list}
    return render(request, 'edit_customer.html',context)


def process_edit(request,customer_id):
    if request.POST:
        name = request.POST.get('name')
        telephone = request.POST.get('telephone')
        group = request.POST.get('group')
        location = request.POST.get('location')
        active = request.POST.get('active')

        
        customer = Customer.objects.get(id=customer_id)
        customer.name=name
        customer.telephone=telephone
        customer.group_id=group
        customer.location_id=location      
        customer.updated_at=date.today()
        customer.active=active
        customer.save() 
        message = name + " " + _("edited_success")
        messages.add_message(request, messages.SUCCESS, message)   
        cache.delete('customers_list_json')
        
    return HttpResponseRedirect('/customer/edit/'+customer_id)  

def process_delete(request,customer_id):
    customer = Customer.objects.get(id=customer_id)
    message = customer.name + " " + _("deleted_success")
    messages.add_message(request, messages.SUCCESS, message)   
    customer.delete()
    cache.delete('customers_list_json')
    return HttpResponseRedirect('/customer/list')  

@login_required(redirect_field_name=None)
def customer_list(request):
    customers_list = Customer.objects.all().order_by('-id')
    paginator = Paginator(customers_list, 20)
    
    page = request.GET.get('page')
    try:
        customers_list = paginator.page(page)
    except PageNotAnInteger:
        customers_list = paginator.page(1)
    except EmptyPage:
        customers_list = paginator.page(paginator.num_pages)
    
    
    context = {'customers_list': customers_list}
    

    return render(request, 'customer_list.html',context)

def customer_list_json(request):
    
    customers_list_json = cache.get('customers_list_json')
    if not customers_list_json:      
        customers_data = Customer.objects.select_related().all()
        customers_list=[]
        data=[]
        for customer in customers_data:
            data={"name":customer.name,"group":customer.group.name,"location":customer.location.name,"telephone":customer.telephone}
            customers_list.insert(0,data)
        customers_list_json=json.dumps(customers_list)
        cache.set('customers_list_json',customers_list_json)
    
       
    return HttpResponse(customers_list_json, mimetype="application/json")
   
   